
log using "YOUR LOCATION\manuscript figure 1 and 2.smcl", replace

use "YOUR LOCATION\generate pss.dta", clear

set more off 

***Replication do-file: Adapt or perish
***Replication of party system saturation measure (Figure 1 and 2)
***Author: Marc van de Wardt
***E-mail: M.vdWardt@uvt.nl
***Date: 27-2-2019

***Variable description
label var country  "Country name"
label var ccode  "Country code"
label var elec_year "Election year"
label var elec_month "Election month"
label var enep "Effective number of electoral parties t (Bormann and Golder 2013, Version 2.0)"
label var LogMedian "Logged Median District Magnitude t (Carey and Hix 2011 with own additions for recent elections)"
label var cdiv "Cultural fractionalization t (Fearon 2003)"
label var issue_diversity "Issue diversity party system agenda t (Own calculations, based on Comparative Manifesto Project)"
label var raw_dimens "Raw dimensionality t (Own calculations, based on procedures in Stoll (2010)"
label var estdem "Established democracies (1 if countries first received a Polity IV freedom score of 6 or higher before 1989)"
label var bjpols "1 if part of country selection for adapt or perish paper"
label var disagr_weight_wvs "Fractionalization of voter preferences along left-right dimension (Own calculations, based on Eurobarometer and WVS)"
label var pss_cg "Party system saturation t (based on residuals from Table 2, Model established democracies of Clark and Golder (2006)"
label var iso3_abbr "Iso3 label"

***Replicate Van de Wardt (2017) Model 2, Table 2
reg enep c.LogMedian##c.cdiv  c.LogMedian##c.issue_diversity c.LogMedian##c.raw_dimens if estdem==1, robust cluster(ccode)
predict yhat, xb
predict pss, r

***Replicate Van de Wardt (2017) Model 3, Table 2
reg enep c.LogMedian##c.cdiv  c.LogMedian##c.issue_diversity c.LogMedian##c.raw_dimens c.LogMedian##c.disagr_weight_wvs if estdem==1, robust cluster(ccode)
predict pss_mod3, r

***Replicate Figure 2
gen lab=iso3_abbr
egen lab2=concat(lab elec_year)

preserve
drop if yhat==.

bys ccode: gen index2=_n
bys ccode: gen tot=_N
gen lab3=lab2 if  index2==tot

keep if index2==tot

scatter enep yhat if bjpols==1, legend(off) mcolor(black) msymbol(diamond)  mlab(lab3) mlabcolor(black)  xtitle("Predicted effective number of parties") ytitle("Observed effective number of parties") title("")  graphregion(fcolor(white)) || function y=x, range(2 11) lcolor(black) name(sat, replace)
capture graph export "YOUR LOCATION\Figure2.wmf", replace

restore

***Replicate Figure 1
qui eststo one: reg enep c.LogMedian##c.cdiv  c.LogMedian##c.issue_diversity c.LogMedian##c.raw_dimens if estdem==1, robust cluster(ccode)

coefplot(one, mcolor(black) msymbol(square)  label(Model 1)) , ///
drop(_cons) xline(0, lcolor(black)) xtitle(B coefficient) mlcolor(white) ciopts(lwidth(*1) lcolor(black)) levels(90)  ///
coeflabels(LogMedian="logM" cdiv="Societal heterogeneity" c.LogMedian#c.cdiv="logM*Societal heterogeneity" raw_dimens="Issue dimensionality" c.LogMedian#c.raw_dimens="Issue dimensionality*LogM" issue_diversity="Issue diversity" c.LogMedian#c.issue_diversity="Issue diversity*LogM") ///
graphregion(fcolor(white)) name(two, replace)
capture graph export "YOUR LOCATION\Figure1.wmf", replace

gen yline=0 

reg enep c.LogMedian##c.cdiv c.LogMedian##c.issue_diversity c.LogMedian##c.raw_dimens if estdem==1, robust cluster(ccode)
margins, dydx(cdiv) at(LogMedian=(0(0.1)5)) l(90)

gen xyz=1 if e(sample)==1
matrix y_=r(table)'
svmat y_
range g 0 5 52

twoway hist LogMedian if xyz==1, percent bcolor(gs15) yaxis(2)  ///
|| line y_5 y_6 g,  lcolor(black) lpattern(dash dash)  ///
|| line y_1 g, lcolor(black) ///
|| line yline g, lcolor(black) ///
,  xtitle("Log of median district magnitude",  size(medsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
ytitle("% of observations", size(medsmall) axis(2)) ///
ytitle("Marg. effect of societal heterogeneity", size(medsmall) axis(1)) ///
legend(off)  graphregion(color(white)) name(a, replace)

drop xyz- g

margins, dydx(raw_dimens) at(LogMedian=(0(0.1)5)) l(90)

gen xyz=1 if e(sample)==1
matrix y_=r(table)'
svmat y_
range g 0 5 52

twoway hist LogMedian if xyz==1, percent bcolor(gs15) yaxis(2)  ///
|| line y_5 y_6 g,  lcolor(black) lpattern(dash dash)  ///
|| line y_1 g, lcolor(black) ///
|| line yline g, lcolor(black) ///
,  xtitle("Log of median district magnitude",  size(medsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
ytitle("% of observations", size(medsmall) axis(2)) ///
ytitle("Marg. effect of issue dimensionality", size(medsmall) axis(1)) ///
legend(off)  graphregion(color(white)) name(b, replace)

drop xyz-g

margins, dydx(issue_diversity) at(LogMedian=(0(0.1)5)) l(90)

gen xyz=1 if e(sample)==1
matrix y_=r(table)'
svmat y_
range g 0 5 52

twoway hist LogMedian if xyz==1, percent bcolor(gs15) yaxis(2)  ///
|| line y_5 y_6 g,  lcolor(black) lpattern(dash dash)  ///
|| line y_1 g, lcolor(black) ///
|| line yline g, lcolor(black) ///
,  xtitle("Log of median district magnitude",  size(medsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
ytitle("% of observations", size(medsmall) axis(2)) ///
ytitle("Marg. effect of issue diversity", size(medsmall) axis(1)) ///
legend(off)  graphregion(color(white)) name(c, replace)

drop xyz- g

graph combine two a b c, graphregion(fcolor(white))  altshrink
capture graph export "YOUR LOCATION\Figure1.wmf", replace

**Replicate Figure A3

graph combine a b c, graphregion(fcolor(white))  altshrink
capture graph export "YOUR LOCATION\FigureA3.wmf", replace

**Replicate Figure A4

reg enep c.LogMedian##c.cdiv  c.LogMedian##c.issue_diversity c.LogMedian##c.raw_dimens c.LogMedian##c.disagr_weight_wvs if estdem==1, robust cluster(ccode)
margins, dydx(cdiv) at(LogMedian=(0(0.1)5)) l(90)

gen xyz=1 if e(sample)==1
matrix y_=r(table)'
svmat y_
range g 0 5 52

twoway hist LogMedian if xyz==1, percent bcolor(gs15) yaxis(2)  ///
|| line y_5 y_6 g,  lcolor(black) lpattern(dash dash)  ///
|| line y_1 g, lcolor(black) ///
|| line yline g, lcolor(black) ///
,  xtitle("Log of median district magnitude",  size(medsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
ytitle("% of observations", size(medsmall) axis(2)) ///
ytitle("Marg. effect of societal heterogeneity", size(medsmall) axis(1)) ///
legend(off)  graphregion(color(white)) name(a, replace)

drop xyz- g

margins, dydx(raw_dimens) at(LogMedian=(0(0.1)5)) l(90)

gen xyz=1 if e(sample)==1
matrix y_=r(table)'
svmat y_
range g 0 5 52

twoway hist LogMedian if xyz==1, percent bcolor(gs15) yaxis(2)  ///
|| line y_5 y_6 g,  lcolor(black) lpattern(dash dash)  ///
|| line y_1 g, lcolor(black) ///
|| line yline g, lcolor(black) ///
,  xtitle("Log of median district magnitude",  size(medsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
ytitle("% of observations", size(medsmall) axis(2)) ///
ytitle("Marg. effect of issue dimensionality", size(medsmall) axis(1)) ///
legend(off)  graphregion(color(white)) name(b, replace)

drop xyz-g

margins, dydx(issue_diversity) at(LogMedian=(0(0.1)5)) l(90)

gen xyz=1 if e(sample)==1
matrix y_=r(table)'
svmat y_
range g 0 5 52

twoway hist LogMedian if xyz==1, percent bcolor(gs15) yaxis(2)  ///
|| line y_5 y_6 g,  lcolor(black) lpattern(dash dash)  ///
|| line y_1 g, lcolor(black) ///
|| line yline g, lcolor(black) ///
,  xtitle("Log of median district magnitude",  size(medsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
ytitle("% of observations", size(medsmall) axis(2)) ///
ytitle("Marg. effect of issue diversity", size(medsmall) axis(1)) ///
legend(off)  graphregion(color(white)) name(c, replace)

drop xyz- g

margins, dydx(disagr_weight_wvs) at(LogMedian=(0(0.1)5)) l(90)

gen xyz=1 if e(sample)==1
matrix y_=r(table)'
svmat y_
range g 0 5 52

twoway hist LogMedian if xyz==1, percent bcolor(gs15) yaxis(2)  ///
|| line y_5 y_6 g,  lcolor(black) lpattern(dash dash)  ///
|| line y_1 g, lcolor(black) ///
|| line yline g, lcolor(black) ///
,  xtitle("Log of median district magnitude",  size(medsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
ytitle("% of observations", size(medsmall) axis(2)) ///
ytitle("Marg. effect of voter lr fractionalization", size(medsmall) axis(1)) ///
legend(off)  graphregion(color(white)) name(d, replace)

drop xyz- g

graph combine a b c d, graphregion(fcolor(white))  altshrink
capture graph export "YOUR LOCATION\FigureA4.wmf", replace

**Save party system saturation measures

preserve

keep ccode elec_year elec_month pss pss_mod3 pss_cg

bys ccode elec_year elec_month: gen newid=_N
drop if newid>1
drop newid

capture save "YOUR LOCATION\PSS measures.dta", replace

restore

log close
